<?php
class ModelController extends BaseController
{
	public function addModel()
	{
		$modelName    =	Input::get('modelName');
		$tableName    =	Input::get('tableName');
		$fieldNames   =	Input::get('fieldNames');
		$fieldTypes   =	Input::get('fieldTypes');
		$displayNames =	Input::get('displayNames');
		$validator = Validator::make(
		    array('modelName' => $modelName , 'tableName'=> $tableName ),
		    array('modelName' => 'required|unique:model,model_name' , 'tableName'=>'required|unique:model,table_name|alpha_dash')
		);

		if(count($fieldNames)!==count($fieldTypes) && count($fieldNames)!==count($displayNames))
		{
			return Response::json( CMSException::$addModelErr);
		}

		if($validator->fails())
		{
			return Response::json( CMSException::$addModelErr);
		}

		for($i=0;$i<count($fieldNames);$i++)
		{
			for($j=$i+1;$j<count($fieldNames);$j++)
			{
				if($fieldNames[$i]==$fieldNames[$j])
				{
					return Response::json( CMSException::$addModelErr);
				}
				else if($displayNames[$i]==$displayNames[$j])
				{
					return Response::json( CMSException::$addModelErr);
				}
			}

			$validator = Validator::make(
			    array('fieldName' => $fieldNames[$i] , 'fieldType'=> $fieldTypes[$i] , 'displayName'=> $displayNames[$i]),
			    array('fieldName' => 'required|alpha_dash' , 'fieldType'=>'required|exists:type,type_name' , 'displayName'=>'required')
			);
			
			if($validator->fails())
			{
				return Response::json( CMSException::$addModelErr);
			}
		}

		$modelService =	new ModelService;
		$modelService->setModelName($modelName);
		$modelService->setTableName($tableName);
		$modelService->setFieldNames($fieldNames);
		$modelService->setFieldTypes($fieldTypes);
		$modelService->setDisplayNames($displayNames);

		$errCode =	$modelService->addModel();


		return Response::json($errCode);
	}

	public function deleteModel()
	{
		$modelId      =Input::get('modelId');
		$validator = Validator::make(
		    array('modelId' => $modelId ),
		    array('modelId' => 'required|exists:model,id' )
		);

		if($validator->fails())
		{
			return Response::json( CMSException::$deleteModelErr);
		}

		$modelService =	new ModelService;
		$modelService->setModelId($modelId);
		$errCode      =$modelService->deleteModel();
		return Response::json($errCode);
	}

	public function updateModel()
	{
		$modelId      =Input::get('modelId');
		$modelName    =Input::get('modelName');

		$modelService =	new ModelService;
		$modelService->setModelId($modelId);
		$model        =$modelService->findById();

		if($model['errCode']==9)
		{
			return Response::json( CMSException::$updateModelErr);
		}
		if($model->model_name!==$modelName)
		{
			$validator = Validator::make(
			    array('modelName' => $modelName),
			    array('modelName' => 'required|unique:model,model_name')
			);
		
			if($validator->fails())
			{
				return Response::json( CMSException::$updateModelErr);
			}
		}


		$modelService =	new ModelService;
		$modelService->setModelId($modelId);
		$modelService->setModelName($modelName);
		$errCode      =$modelService->updateModel();
		return Response::json($errCode);
	}

	public function findAll()
	{
		$modelService =	new ModelService;
		$models       =$modelService->findAll();
		return Response::json($models);
	}

	public function findById()
	{
		$modelId      =Input::get('modelId');
		$modelService =	new ModelService;
		$modelService->setModelId($modelId);
		$model        =$modelService->findById();
		return Response::json($model);;
	}
}